package com.dhwl.hsh.driver.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dhwl.hsh.lib.utils.DateUtil;
import com.dhwl.hsh.lib.view.slidedatetimepicker.SlideDatePicker;
import com.dhwl.hsh.lib.view.slidedatetimepicker.SlideDateTimeListener;
import com.dhwl.hsh.lib.view.slidedatetimepicker.SlideTimeListener;
import com.dhwl.hsh.lib.view.slidedatetimepicker.SlideTimePicker;
import com.dhwl.hsh.driver.R;
import com.dhwl.hsh.driver.common.Constant;
import com.dhwl.hsh.driver.view.AddSubtracterEditView;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 选择用车时间
 * @author Derek.Kidd
 * @time 2016/4/28 14:39
 */
public class ChooseTimeActivity extends BaseActivity {

    //按天、按频率
    @Bind(R.id.layout_choose_day)
    LinearLayout layoutChooseDay;
    @Bind(R.id.layout_choose_interval)
    LinearLayout layoutChooseInterval;
    @Bind(R.id.rg_type_shipping)
    RadioGroup rgTypeShipping;
    @Bind(R.id.rb_type_day)
    RadioButton rbTypeDay;
    @Bind(R.id.rb_type_period)
    RadioButton rbTypePeriod;
    //星期几
    @Bind(R.id.btn_week_1)
    Button btnWeek1;
    @Bind(R.id.btn_week_2)
    Button btnWeek2;
    @Bind(R.id.btn_week_3)
    Button btnWeek3;
    @Bind(R.id.btn_week_4)
    Button btnWeek4;
    @Bind(R.id.btn_week_5)
    Button btnWeek5;
    @Bind(R.id.btn_week_6)
    Button btnWeek6;
    @Bind(R.id.btn_week_7)
    Button btnWeek7;
    //间隔多少天
    @Bind(R.id.ase_interval)
    AddSubtracterEditView aseInterval;
    //开始日期和时间
    @Bind(R.id.layout_start_date)
    RelativeLayout layoutStartDate;
    @Bind(R.id.layout_start_time)
    RelativeLayout layoutStartTime;
    @Bind(R.id.tv_start_date)
    TextView tvStartDate;
    @Bind(R.id.tv_start_time)
    TextView tvStartTime;


    @Bind(R.id.btn_sure)
    Button btnSure;

    /**
     * day 0,
     */
    private int shippingType = Constant.Order.SHIPPING_TIME_TYPE_DAY;

    private HashMap<Integer, Boolean> weeks;

    private String startDate;

    private String transformTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_time;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setTitleContent("选择时间");
        rgTypeShipping.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_type_day) {
                    layoutChooseDay.setVisibility(View.VISIBLE);
                    layoutChooseInterval.setVisibility(View.GONE);
                    shippingType = Constant.Order.SHIPPING_TIME_TYPE_DAY;
                } else if (checkedId == R.id.rb_type_period) {
                    layoutChooseInterval.setVisibility(View.VISIBLE);
                    layoutChooseDay.setVisibility(View.GONE);
                    shippingType = Constant.Order.SHIPPING_TIME_TYPE_PERIOD;
                }
            }
        });

        aseInterval.setMaxNumber(Constant.Order.SHIPPING_TIME_MAX_INTERVAL);
    }

    @Override
    protected void initData() {
        weeks = new HashMap<>();
        for (int i = 1; i < 8; i++) {
            weeks.put(i, false);
        }
    }


    @OnClick({R.id.btn_week_1, R.id.btn_week_2, R.id.btn_week_3, R.id.btn_week_4, R.id.btn_week_5, R.id.btn_week_6, R.id.btn_week_7})
    public void onWeekClick(View view) {
        switch (view.getId()) {
            case R.id.btn_week_1:
                changeWeekBtn(view, Calendar.SUNDAY);
                break;
            case R.id.btn_week_2:
                changeWeekBtn(view, Calendar.MONDAY);
                break;
            case R.id.btn_week_3:
                changeWeekBtn(view, Calendar.TUESDAY);
                break;
            case R.id.btn_week_4:
                changeWeekBtn(view, Calendar.WEDNESDAY);
                break;
            case R.id.btn_week_5:
                changeWeekBtn(view, Calendar.THURSDAY);
                break;
            case R.id.btn_week_6:
                changeWeekBtn(view, Calendar.FRIDAY);
                break;
            case R.id.btn_week_7:
                changeWeekBtn(view, Calendar.SATURDAY);
                break;
        }
    }

    /**
     * 切换星期的状态
     *
     * @param view
     * @param week
     */
    private void changeWeekBtn(View view, int week) {
        weeks.put(week, !weeks.get(week));
        if (weeks.get(week))
            view.setBackgroundResource(R.drawable.shape_btn_bg_week_on);
        else {
            view.setBackgroundResource(R.drawable.shape_btn_bg_week_off);
        }
    }

    private SlideDateTimeListener dateListener = new SlideDateTimeListener() {
        @Override
        public void onDateTimeSet(Date date) {

            if (weeks.get(DateUtil.getWeekOfDate(date))) {
                startDate = DateUtil.dateFormater1.get().format(date);
                tvStartDate.setText(startDate);
            } else {
                showToast("所选日期不在发货时间内");
            }
        }
    };

    private SlideTimeListener timeListener = new SlideTimeListener() {

        @Override
        public void onTimeSet(int hour, int minute) {
            transformTime = hour + ":" + (minute == 0 ? "00" : minute);
            tvStartTime.setText(transformTime);
        }
    };

    @OnClick(R.id.layout_start_date)
    public void onStartDateClick(View view) {
        new SlideDatePicker.Builder(getSupportFragmentManager())
                .setListener(dateListener)
                .setInitialDate(new Date())
                .setMinDate(new Date())
                .build().show();
    }

    @OnClick(R.id.layout_start_time)
    public void onStartTimeClick(View view) {

        new SlideTimePicker.Builder(getSupportFragmentManager())
                .setListener(timeListener)
                .setInitialTime(new Date())
                .setIs24HourTime(true)
                .setInterval(15)
                .build().show();
    }


    @OnClick(R.id.btn_sure)
    public void onSureClick(View view) {
        Intent intent = new Intent();
        intent.putExtra(Constant.Order.SHIPPING_TIME_TYPE_KEY, shippingType);
        //按天 的每周几
        if (shippingType == Constant.Order.SHIPPING_TIME_TYPE_DAY) {
            boolean isChecked = false;
            for (int i = 1; i < 8; i++) {
                if (weeks.get(i)) {
                    isChecked = true;
                    break;
                }
            }
            if (isChecked) {
                intent.putExtra("weeks", weeks);
            } else {
                showToast("请选择每周那几天发货");
                return;
            }
        }
        //按频率的 的每隔几天
        if (shippingType == Constant.Order.SHIPPING_TIME_TYPE_PERIOD) {
            intent.putExtra("interval", aseInterval.getNum());
        }

        if (!TextUtils.isEmpty(startDate)) {
            intent.putExtra("start_date", startDate);
        } else {
            showToast("请选择开始发货日期");
            return;
        }

        if (!TextUtils.isEmpty(transformTime)) {
            intent.putExtra("transform_time", transformTime);
        } else {
            showToast("请选择发货时间");
            return;
        }

        setResult(RESULT_OK, intent);
        finish();
    }
}
